package com.jhd.jiangxi.activity.scan.in.supply;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jhd.jiangxi.R;
import com.jhd.jiangxi.activity.main.BaseActivity;
import com.jhd.jiangxi.vo.DeliveryDtl;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jhd.jiangxi.activity.scan.in.supply.SupplyActivity.electCodePackageVos2;
import static com.jhd.jiangxi.activity.scan.in.supply.SupplyStorageActivity.deliveryDtlList;

/*交货单汇总明细*/
public class SupplyDetailActivity extends BaseActivity {
    @BindView(R.id.btnBack)ImageButton btnBack;
    @BindView(R.id.tvHeaderUserName)TextView tvHeaderUserName;
    @BindView(R.id.rvStorage)RecyclerView rvStorage;
    private RecyclerAdapter adapter;
    @BindView(R.id.lin_number)
    LinearLayout lin_number;
    @BindView(R.id.tv_total)
    TextView tv_total;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_supply_detail);
        ButterKnife.bind(this);
        lin_number.setVisibility(View.VISIBLE);
        initHeader3("交货单汇总明细");
        initData();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rvStorage.setLayoutManager(layoutManager);
    }

    private void initData() {
        int totalNum=0;
        for(int i=0;i<deliveryDtlList.size();i++){
            int num=0;
            deliveryDtlList.get(i).setScan(0);
            for(int j=0;j<electCodePackageVos2.size();j++){
                if(electCodePackageVos2.get(j).getLotNumber().equals(deliveryDtlList.get(i).getLotNumber())){
                    num=num+electCodePackageVos2.get(j).getQuantity();
                    deliveryDtlList.get(i).setScan(num);
                    deliveryDtlList.get(i).setStorageName(electCodePackageVos2.get(j).getStorage());
                }
            }
        }
        for (DeliveryDtl deliveryDtl : deliveryDtlList) {
            totalNum=totalNum+deliveryDtl.getScan();
        }
        tv_total.setText(String.valueOf(totalNum));
        adapter=new RecyclerAdapter(deliveryDtlList);
        rvStorage.setAdapter(adapter);
    }

    public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
        private List<DeliveryDtl> deliveryDtlList;

        public RecyclerAdapter( List<DeliveryDtl> deliveryDtlList) {
            this.deliveryDtlList = deliveryDtlList;
        }

        @NonNull
        @Override
        public RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_scan_detail_1, parent, false);
            RecyclerAdapter.ViewHolder viewHolder = new RecyclerAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerAdapter.ViewHolder holder, int position) {
            if(deliveryDtlList.get(position).getScan()!=deliveryDtlList.get(position).getQuantity()){
                holder.tvVaccineName.setText(deliveryDtlList.get(position).getVaccineName()+"(未入库)");
            }else{
                holder.tvVaccineName.setText(deliveryDtlList.get(position).getVaccineName());
            }
            holder.tvScanNo.setText(String.valueOf(deliveryDtlList.get(position).getScan()));
            holder.tvNum.setText(String.valueOf(deliveryDtlList.get(position).getQuantity()));
            holder.tvLotNumber.setText(String.valueOf(deliveryDtlList.get(position).getLotNumber()));
            holder.tvStorage.setText(String.valueOf(deliveryDtlList.get(position).getStorageName()));
//
        }

        @Override
        public int getItemCount() {
            return deliveryDtlList.size();
        }


        class ViewHolder extends RecyclerView.ViewHolder{
            TextView tvVaccineName,tvScanNo,tvNum,tvLotNumber,tvStorage; //疫苗名称，扫码数，交货单数
            public ViewHolder(View itemView) {
                super(itemView);
                this.tvVaccineName = itemView.findViewById(R.id.tvVaccineName);
                this.tvScanNo = itemView.findViewById(R.id.tvScanNo);
                this.tvNum = itemView.findViewById(R.id.tvNum);
                this.tvStorage = itemView.findViewById(R.id.tvStorage);
                this.tvLotNumber = itemView.findViewById(R.id.tvLotNumber);
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}